import React, { Component } from 'react';

// 创建 context 对象
const UserContext = React.createContext({ nickname:"默认",context: -1 })

class ProfileHeader extends Component {
  render() {
    return (
      <div>
        <h2>用户昵称: {this.context.nickname}</h2>
        <h2>用户等级: {this.context.level}</h2>
      </div>
    )
  }
}
ProfileHeader.contextType = UserContext;
class Profile extends Component {
  render() {
    return (
      <div>
        <ProfileHeader />
        <ul>
          <li>设置1</li>
          <li>设置2</li>
          <li>设置3</li>
          <li>设置4</li>
          <li>设置5</li>
        </ul>
      </div>
    )
  }
}

class App extends Component {
  render() {
    return (
      <div>
        <UserContext.Provider value={{ nickname:"why", level: 99 }}>
          <Profile />
        </UserContext.Provider>
      </div>
    );
  }
}

export default App;